1 00:00:00,770 --> 00:00:06,530 In this lecture, we're going to be scripting the next trap in our RB, which are small tripwires that 2 00:00:06,530 --> 00:00:10,130 can knock you over and cause you to fall to your death. 3 00:00:10,640 --> 00:00:12,020 So let's get started. 4 00:00:12,880 --> 00:00:17,980 So we're just going to follow the exact same procedure that we did for the wood break handler is that 5 00:00:17,980 --> 00:00:20,110 we're going to create a new module script. 6 00:00:20,110 --> 00:00:24,160 The name for this module script is going to be my tripwire handler. 7 00:00:24,280 --> 00:00:26,290 And then I'm going to use the same template. 8 00:00:26,290 --> 00:00:32,230 Once again paste that in here and I'm going to rename this to my tripwire handler. 9 00:00:32,230 --> 00:00:37,930 And we're only going to need an initialize function because we're going to get the collection service 10 00:00:37,930 --> 00:00:42,670 and grab all of the tripwires in our game that have the tag of tripwire. 11 00:00:43,820 --> 00:00:46,940 So I can go ahead and define my tag. 12 00:00:47,520 --> 00:00:50,670 Which is just the string of tripwire. 13 00:00:51,240 --> 00:00:57,570 And then we can go ahead and listen to the collection service, get instance added signal, pass my 14 00:00:57,570 --> 00:01:00,720 tag and then go ahead and get my instance. 15 00:01:00,720 --> 00:01:05,400 And this is where we're going to use our class to create new tripwire objects. 16 00:01:06,030 --> 00:01:11,640 Now inside of my RB model two you can see that I have a tripwire in here tagged with tripwire. 17 00:01:11,640 --> 00:01:16,650 And inside of it it's very simple, just some parts and a detect part that we're going to listen for 18 00:01:16,650 --> 00:01:20,820 when a player touches and when our player touches it, we're going to trip them over. 19 00:01:22,050 --> 00:01:26,850 So we can go ahead and create another new module script that's going to be a child of our tripwire handler. 20 00:01:26,850 --> 00:01:30,510 And we're going to call this one our tripwire class. 21 00:01:31,090 --> 00:01:32,080 And inside of here. 22 00:01:32,080 --> 00:01:32,620 Same deal. 23 00:01:32,620 --> 00:01:39,280 We're going to use our class template and I'm going to name this to tripwire class. 24 00:01:39,280 --> 00:01:43,420 And I'll just rename this table here to Tripwire Objects. 25 00:01:44,420 --> 00:01:47,840 Then we can go ahead and get my class inside of my tripwire handler. 26 00:01:47,840 --> 00:01:49,910 So I'll call it tripwire class. 27 00:01:49,910 --> 00:01:53,510 And that's equal to require script dot tripwire class. 28 00:01:53,510 --> 00:01:59,600 So anytime a new tripwire is added into the workspace, we're going to use the tripwire dot new constructor 29 00:01:59,600 --> 00:02:01,460 and pass that instance. 30 00:02:02,070 --> 00:02:05,160 Inside our tripwire class, we're going to need a couple services. 31 00:02:05,160 --> 00:02:07,350 One is going to be the player service. 32 00:02:09,290 --> 00:02:12,260 And then we're also going to need the sound service. 33 00:02:15,890 --> 00:02:18,650 And that's because we're going to make a reference to a couple of things. 34 00:02:18,650 --> 00:02:22,820 First, we want to get the local player which is equal to players dot local player. 35 00:02:23,270 --> 00:02:26,510 We're going to get the tripwire sound that's inside of the sound service. 36 00:02:26,510 --> 00:02:29,810 So that's in the SFX folder and it's called tripwire. 37 00:02:30,770 --> 00:02:35,120 And then we can go ahead and get a random generator. 38 00:02:35,120 --> 00:02:36,920 So random dot new. 39 00:02:37,860 --> 00:02:42,210 And then we can also define a constant for this class. 40 00:02:42,210 --> 00:02:44,850 And I'm going to call this constant cooldown. 41 00:02:44,850 --> 00:02:50,190 And this is going to represent how long we should wait before the tripwire should be able to trip a 42 00:02:50,190 --> 00:02:51,000 player again. 43 00:02:51,000 --> 00:02:53,040 And we're going to set it to like two seconds. 44 00:02:53,040 --> 00:02:59,010 So that way when the player touches the tripwire, they aren't like tripped multiple times instantaneously, 45 00:02:59,010 --> 00:02:59,190 right? 46 00:02:59,190 --> 00:03:01,050 We want to add a little bit of a cooldown. 47 00:03:01,410 --> 00:03:05,190 Now inside of our constructor we can go ahead and make a reference to our object. 48 00:03:05,840 --> 00:03:11,360 And then we can go ahead and have that same kind of debounce that we had in our wood break class. 49 00:03:11,360 --> 00:03:16,070 So we'll call this on cooldown and by default it's going to be equal to false. 50 00:03:16,070 --> 00:03:22,160 And then we're going to listen for when the detect zone part in our model is touched. 51 00:03:22,730 --> 00:03:24,980 We'll get that other part. 52 00:03:25,790 --> 00:03:29,120 And then we can go ahead and check to see if a player touched it. 53 00:03:29,120 --> 00:03:34,460 So we're going to do players get player from character pass other part dot parent. 54 00:03:35,930 --> 00:03:39,170 And if there isn't a player, then we're just going to return. 55 00:03:39,170 --> 00:03:45,560 Otherwise we can just trip ourselves so we can go ahead and add a function to the meta table. 56 00:03:45,800 --> 00:03:48,950 And we can call this function trip player. 57 00:03:49,310 --> 00:03:55,820 And because this is going to be run on the client, the only player we can really trip here is the local 58 00:03:55,820 --> 00:03:56,330 player. 59 00:03:56,330 --> 00:04:02,030 So you could define a player to be passed to this function. 60 00:04:02,030 --> 00:04:05,300 Or you could just leave it blank and just trip the local player. 61 00:04:05,300 --> 00:04:06,170 By default. 62 00:04:06,170 --> 00:04:11,150 It doesn't really matter whatever you'd like to do, and we're going to be calling this function from 63 00:04:11,150 --> 00:04:13,220 the lambda function connected to the touched event. 64 00:04:13,220 --> 00:04:15,950 So we could do self trip player. 65 00:04:16,130 --> 00:04:18,710 And that means we got to make sure we aren't on a cooldown. 66 00:04:18,710 --> 00:04:24,860 So if self dot cooldown is true then we're just going to return. 67 00:04:24,860 --> 00:04:29,390 Otherwise we can set self to on cooldown equal to true. 68 00:04:30,350 --> 00:04:33,650 And then what we're going to do is we're going to clone that tripwire sound. 69 00:04:37,980 --> 00:04:44,730 We're going to set the parent of that sound equal to, um, we'll set it to the wire part that's inside 70 00:04:44,730 --> 00:04:45,450 of our trip wire. 71 00:04:45,450 --> 00:04:48,660 So that'll be self dot model dot wire. 72 00:04:48,750 --> 00:04:51,570 And then we'll set play on remove equal to true. 73 00:04:51,570 --> 00:04:54,630 And then we will destroy this clone. 74 00:04:54,630 --> 00:04:56,130 So that way it plays a sound. 75 00:04:56,130 --> 00:04:58,470 And then it cleans it up for us automatically. 76 00:04:58,650 --> 00:05:02,430 And then we can go ahead and get the humanoid from our player. 77 00:05:02,430 --> 00:05:06,000 So that would be, uh, local player. 78 00:05:06,710 --> 00:05:12,170 Dot character and find first child, which is a humanoid. 79 00:05:13,530 --> 00:05:17,010 And then we'll also get the root part of our players character. 80 00:05:17,010 --> 00:05:22,770 So that's equal to local player dot character dot humanoid root part. 81 00:05:23,340 --> 00:05:26,070 And this is because we need to change a state in our humanoid. 82 00:05:26,070 --> 00:05:27,930 So we'll use the change state function. 83 00:05:27,930 --> 00:05:32,160 And we want to change it to the humanoid state type of falling down. 84 00:05:32,160 --> 00:05:38,700 So we trip our player and then to basically have them fall forward, we can apply a small force to the 85 00:05:38,700 --> 00:05:40,890 root parts assembly linear velocity. 86 00:05:40,890 --> 00:05:45,240 So root part dot assembly linear velocity. 87 00:05:45,240 --> 00:05:47,640 That's going to be equal to. 88 00:05:48,470 --> 00:05:51,830 Um, we'll actually just add it to itself. 89 00:05:51,830 --> 00:05:57,710 That's going to be equal to the root part dot c frame dot look vector. 90 00:05:57,950 --> 00:06:00,500 And we can multiply this value by whatever we want. 91 00:06:00,530 --> 00:06:02,900 So maybe like a value of 20. 92 00:06:03,600 --> 00:06:11,400 And then what we could do is wait or yield for that cool down to set on, cool down, back to false, 93 00:06:11,400 --> 00:06:13,920 to trip up ourselves again. 94 00:06:14,830 --> 00:06:18,250 That's really all we need to do for this particular tripwire trap. 95 00:06:18,280 --> 00:06:24,460 So any time a tripwire is added to our game because we clone a model and parent it to the workspace, 96 00:06:24,460 --> 00:06:30,160 then we're going to set up that tripwire and trip our local player when they touch that detect zone 97 00:06:30,160 --> 00:06:30,940 apart. 98 00:06:31,750 --> 00:06:37,960 So now we can actually just go and play, test the game and make sure everything works perfectly. 99 00:06:38,290 --> 00:06:43,870 And let's see if we generate any models that have the tripwire inside of it. 100 00:06:43,870 --> 00:06:48,670 So I'll walk around a little bit here and I'll catch you when I find one. 101 00:06:49,510 --> 00:06:49,960 All right. 102 00:06:49,960 --> 00:06:51,820 Here's one of the tripwire models. 103 00:06:51,820 --> 00:06:57,790 And if I go and walk and touch it, as you can see, it knocked me forward right on my face. 104 00:06:57,790 --> 00:07:00,190 And if I go and touch it again, there you go. 105 00:07:00,190 --> 00:07:02,650 I tripped again, so pretty neat. 106 00:07:02,650 --> 00:07:05,290 As we walk forward, it trips us on our face. 107 00:07:05,290 --> 00:07:10,960 And this would be a killer combo if a bridge spawned in front of this tripwire. 108 00:07:10,960 --> 00:07:15,520 So if you did not see this tripwire and there was a bridge in front of you, you would trip fall on 109 00:07:15,520 --> 00:07:16,330 those wood. 110 00:07:16,330 --> 00:07:19,570 Those wood pieces would break and you would fall to your death. 111 00:07:19,570 --> 00:07:24,100 So a cool little hidden trap that's also a little bit annoying for our players. 112 00:07:25,490 --> 00:07:28,880 That's all for this lecture, and I'll catch you in the next one.